from django.http import HttpResponse
from django.shortcuts import render

# Create your views here.
from django.utils.decorators import method_decorator
from django.views import View


class TemplateDemoView(View):
    """演示模板的使用"""
    def get(self, request):
        """提供模板"""
        # 构造上下文
        content = {
            'name':'zxc',
            'alist':[1,2,3],
            'adict':{
                'age':2.3
            }
        }
        #　渲染并响应模板
        return render(request,'index.html',content)

# 装饰器
def my_decorator(view_func):
    """
    类视图装饰器
    :param view_func:被装饰的视图函数
    :return: wrapper
    """
    def wrapper(request,*args,**kwargs):
        print('装饰器被调用了')
        print(request.method,request.path)

        #　调用被装饰的视图函数
        return view_func(request,*args,**kwargs)
    return wrapper


#　第二种装饰
@method_decorator(my_decorator,name = 'get')
class DefineClassview(View):
    """类视图的定义和使用"""
    def get(self,request):
        """GET请求业务逻辑"""
        print('视图函数被调用')
        return HttpResponse('GET请求业务逻辑')


    def post(self,request):
        """POST请求业务逻辑"""
        return HttpResponse('POST请求业务逻辑')


